/*
	This file is part of AVRCppLib.

    AVRCppLib is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    AVRCppLib is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with AVRCppLib.  If not, see <http://www.gnu.org/licenses/>.

    Copyright (C) Maxime Lemonnier 2007-2011
 */

void SPIHardware::enable()
{
	setBit(*spcr, SPE);
}
void SPIHardware::disable()
{
	clearBit(*spcr, SPE);
}

void SPIHardware::enableInterrupts()
{
	setBit(*spcr, SPIE);
}

void SPIHardware::disableInterrupts()
{
	clearBit(*spcr, SPIE);
}

bool SPIHardware::readInterruptFlag()
{
	return bitIsSetInRegister(*spsr, SPIF);
}

void SPIHardware::clearInterruptFlag()
{
	setBit(*spsr, SPIF);
}

void SPIHardware::transmitLSBFirst()
{
	setBit(*spcr, DORD);
}
void SPIHardware::transmitMSBFirst()
{
	clearBit(*spcr, DORD);
}
void SPIHardware::setMaster()
{
	setBit(*spcr, MSTR);
}
void SPIHardware::setSlave()
{
	clearBit(*spcr, MSTR);
}

void SPIHardware::setSPIPinsDirections(DataDirection slaveSelectPinDirection, DataDirection sckPinDirection, DataDirection mosiPinDirection, DataDirection misoPinDirection)
{
	changeBitValueTo(*spiDDR, ssPinNumber, slaveSelectPinDirection);
	changeBitValueTo(*spiDDR, sckPinNumber, sckPinDirection);
	changeBitValueTo(*spiDDR, mosiPinNumber, mosiPinDirection);
	changeBitValueTo(*spiDDR, misoPinNumber, misoPinDirection);
}
void SPIHardware::setSCKHighWhenIdle()
{
	setBit(*spcr, CPOL);
}
void SPIHardware::setSCKLowWhenIdle()
{
	clearBit(*spcr, CPOL);
}
void SPIHardware::sampleDataOnSCKLeadingEdge()
{
	setBit(*spcr, CPHA);
}
void SPIHardware::sampleDataOnSCKTrailingEdge()
{
	clearBit(*spcr, CPHA);
}
void SPIHardware::setPrescaler(Prescaler prescaler, bool doubleBitRate)
{
	applyMaskedConfig(*spcr, 0x03, prescaler);

	changeBitValueTo(*spsr, SPI2X, doubleBitRate);

}
bool SPIHardware::checkWriteCollision()
{
	return bitIsSetInRegister(*spsr, WCOL);
}
uint8_t SPIHardware::readByte()
{
	return *spdr;
}
void SPIHardware::writeByte(uint8_t byte)
{
	*spdr = byte;
}
